package com.canyou.redmine.network;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.canyou.redmine.model.Issue;
import com.canyou.redmine.model.NewIssue;
import com.canyou.redmine.util.CanYouKit;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;


/**
 * API接口
 *
 * @author Administrator on 2016/3/25.
 */
public class CanYouAPI {

    /**
     * 用户登录
     *
     * @param username        用户名
     * @param password        密码
     * @param responseHandler
     */
    public static void login(String username, String password,
                             AsyncHttpResponseHandler responseHandler) {
        RequestParams params = new RequestParams();
        params.put("username", username);
        params.put("password", password);
        HttpKit.get(CanYouUrl.login, params, responseHandler);
    }

    /**
     * 获取项目列表
     *
     * @param responseHandler
     */
    public static void projects(int pageIndex, int pageSize,
                                AsyncHttpResponseHandler responseHandler) {
        RequestParams params = CanYouKit.getBaseParams();
        params.put("limit", pageSize);
        params.put("offset", pageIndex * pageSize);
        HttpKit.get(CanYouUrl.projects, params, responseHandler);
    }

    /**
     * 获取项目详情
     *
     * @param projectId       项目id
     * @param responseHandler
     */
    public static void getProject(int projectId,
                                  AsyncHttpResponseHandler responseHandler) {
        RequestParams params = CanYouKit.getBaseParams();
        HttpKit.get(CanYouUrl.project.replace("project_id", String.valueOf(projectId)),
                params, responseHandler);

    }

    /**
     * 获取项目成员
     *
     * @param projectId       项目ID
     * @param responseHandler
     */
    public static void getMembersByProjectId(int projectId,
                                             AsyncHttpResponseHandler responseHandler) {
        RequestParams params = CanYouKit.getBaseParams();
        params.put("limit", 100);
        HttpKit.get(CanYouUrl.project_memberships.replace("project_id", String.valueOf(projectId)),
                params, responseHandler);

    }

    /**
     * 获取项目耗时情况
     *
     * @param projectId       项目ID
     * @param responseHandler
     */
    public static void getTimeEntriesByProjectId(int projectId,
                                                 AsyncHttpResponseHandler responseHandler) {
        RequestParams params = CanYouKit.getBaseParams();
        params.put("project_id", projectId);
        params.put("limit", 200);
        HttpKit.get(CanYouUrl.project_time_entries, params, responseHandler);

    }

    /**
     * 获取项目问题
     *
     * @param projectId       项目ID
     * @param responseHandler
     */
    public static void getIssuesByProjectId(int projectId,
                                            AsyncHttpResponseHandler responseHandler) {
        RequestParams params = CanYouKit.getBaseParams();
        params.put("limit", 100);
        HttpKit.get(CanYouUrl.project_issues.replace("project_id", String.valueOf(projectId)),
                params, responseHandler);

    }

    /**
     * 获取问题列表
     *
     * @param responseHandler
     */
    public static void issues(AsyncHttpResponseHandler responseHandler) {
        RequestParams params = CanYouKit.getBaseParams();
        HttpKit.get(CanYouUrl.issues, params, responseHandler);
    }

    /**
     * 获取我的任务列表
     *
     * @param pageIndex       页码
     * @param pageSize        分页大小
     * @param responseHandler
     */
    public static void getMyIssues(int pageIndex, int pageSize,
                                   AsyncHttpResponseHandler responseHandler) {
        RequestParams params = CanYouKit.getBaseParams();
        params.put("assigned_to_id", "me");
        params.put("status_id", "open");
        params.put("sort", "id:desc");
        params.put("limit", pageSize);
        params.put("offset", pageIndex * pageSize);
        HttpKit.get(CanYouUrl.issues, params, responseHandler);
    }

    /**
     * 获取问题详情
     *
     * @param id              问题id
     * @param responseHandler
     */
    public static void getIssueById(int id,
                                    AsyncHttpResponseHandler responseHandler) {
        RequestParams params = CanYouKit.getBaseParams();
        HttpKit.get(CanYouUrl.issue.replace("issue_id", String.valueOf(id)), params, responseHandler);
    }

    /**
     * 新建任务单
     *
     * @param context         上下文
     * @param json            任务单
     * @param responseHandler
     */
    public static void createIssue(Context context, String json, AsyncHttpResponseHandler responseHandler) {
        HttpKit.post(context, CanYouUrl.issue_new, json, responseHandler);
    }

    /**
     * 更新任务单
     *
     * @param context         上下文
     * @param issue           任务单
     * @param responseHandler
     */
    public static void updateIssue(Context context, NewIssue issue,
                                   AsyncHttpResponseHandler responseHandler) {
        HttpKit.put(context, CanYouUrl.issue_update
                .replace("issue_id", String.valueOf(issue.getId())), "{\"issue\":" + JSON.toJSONString(issue) + "}", responseHandler);
    }

    /**
     * 获取跟踪 数据项
     *
     * @param responseHandler
     */
    public static void getTrackers(AsyncHttpResponseHandler responseHandler) {
        RequestParams params = CanYouKit.getBaseParams();
        HttpKit.get(CanYouUrl.trackers, params, responseHandler);
    }

    /**
     * 获取任务状态 数据项
     *
     * @param responseHandler
     */
    public static void getIssueSatuses(AsyncHttpResponseHandler responseHandler) {
        RequestParams params = CanYouKit.getBaseParams();
        HttpKit.get(CanYouUrl.issue_statuses, params, responseHandler);
    }

    /**
     * 获取任务优先级 数据项
     *
     * @param responseHandler
     */
    public static void getIssuePriorities(AsyncHttpResponseHandler responseHandler) {
        RequestParams params = CanYouKit.getBaseParams();
        HttpKit.get(CanYouUrl.issue_priorities, params, responseHandler);
    }
}